package com.bangdream.michelia.view.fragment.main.curriculum;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.application.AppCurrentUser;
import com.bangdream.michelia.contract.CoursesContract;
import com.bangdream.michelia.entity.CommentBean;
import com.bangdream.michelia.entity.CoursesTypeBean;
import com.bangdream.michelia.entity.courses.CourseInfoBean;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.presenter.CoursesPresenter;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.view.adapter.CurriculumDataAdapter;
import com.bangdream.michelia.view.adapter.CurriculumEvaluateAdapter;
import com.bangdream.michelia.view.custom.ratingstar.RatingStarView;
import com.bangdream.michelia.view.fragment.currency.BaseFragment;
import com.bangdream.michelia.view.fragment.currency.LazyLoadFragment;

import java.util.List;

/**
 * @date on 2018/7/23 17:51
 * @email 909732184@qq.com
 * 课程详情-评价
 */
public class CurriculumDetailsEvaluate extends LazyLoadFragment<CoursesContract.ICoursesView, CoursesPresenter<CoursesContract.ICoursesView>>
        implements CoursesContract.ICoursesView {
    private View view;
    private XRefreshView refreshView;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private CurriculumEvaluateAdapter adapter;
    private CourseInfoBean data;
    private TextView tvNumber,tvNumber2;
    private TextView btnComment;
    private LinearLayout evaluateTextLayout;
    private RatingStarView starView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.curriculum_evaluate, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    @Override
    public CoursesPresenter createPresenter() {
        return new CoursesPresenter();
    }

    @Override
    public void loadData() {
        if (data == null || mPresenter == null)
            return;
        mPresenter.getCommentListData(0, 0, data.getId(), pd);
    }

    private void init() {
        if (getArguments() != null) {
            data = getArguments().getParcelable("data");
        }
        starView = view.findViewById(R.id.starView);
        evaluateTextLayout = view.findViewById(R.id.evaluateTextLayout);
        tvNumber = view.findViewById(R.id.tvNumber);
        tvNumber2 = view.findViewById(R.id.tvNumber2);
        btnComment = view.findViewById(R.id.btnComment);
        refreshView = view.findViewById(R.id.refreshView);
        refreshView.setPullLoadEnable(false);
        refreshView.setPullRefreshEnable(false);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new CurriculumEvaluateAdapter(getActivity());
        recyclerView.setAdapter(adapter);

        if (data != null && data.isComment()) {
            evaluateTextLayout.setVisibility(View.GONE);
        } else {
            evaluateTextLayout.setVisibility(View.VISIBLE);
        }
        starView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvNumber2.setText((int)starView.getRating()+"分");
            }
        });
        btnComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submitComment((int)(starView.getRating()));
            }
        });

    }

    @Override
    public void setCoursestList(boolean isSuccess, int page, CoursesContract.CoursesType type, List<CoursesBean> data, String stDescribe) {

    }

    @Override
    public void setCoursesDetails(boolean isSuccess, CoursesBean data, String stDescribe) {

    }

    @Override
    public void setCoursesTypeList(boolean isSuccess, List<CoursesTypeBean> data, String stDescribe) {

    }

    @Override
    public void setCommentList(boolean isSuccess, int page, List<CommentBean> data, String stDescribe) {
        if (isSuccess) {
            adapter.setData(data);
            adapter.notifyDataSetChanged();
            tvNumber.setText(data == null ? "(0个评论)" : "(" + data.size() + "个评论)");

        }
    }

    @Override
    public void actionComment(boolean isSuccess, String stDescribe) {
        if (isSuccess) {
            evaluateTextLayout.setVisibility(View.GONE);
            showText(stDescribe);
            loadData(); 
        }
    }

    //提交评价
    public void submitComment(int starNumber) {
        mPresenter.actionComment(data.getId(), "", starNumber, pd);
    }
}
